package client;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;

import javax.naming.NamingException;

import org.ejs.bean.FileManagerRemote;
import org.ejs.bean.ServerManagerRemote;
import org.ejs.network.ServerAddress;

import file.FileClientThread;
import file.FileServerThread;


public class ClientBeanThread extends Thread {
	ServerManagerRemote bean;
	FileManagerRemote file;
	ServerAddress s;
	String  pathLocal;
	public ClientBeanThread(ServerAddress s,String pathLocal){
		this.s = s;
		this.pathLocal = pathLocal;
		javax.naming.Context ic = null;
		try {
			ic = new javax.naming.InitialContext(ClientTools.giveMeJndiProperties());
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		try {
			bean =  (ServerManagerRemote) ic.lookup("ejb:/efs/ServerManager!org.ejs.bean.ServerManagerRemote");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 file = bean.connect(s);
	}
	public ClientBeanThread(ServerAddress s,String pathLocal,ServerManagerRemote bean){
		this.s = s;
		this.bean=bean;
		this.pathLocal = pathLocal;
		file = bean.connect(s);
	}
	
	public ServerManagerRemote getBean() {
		return bean;
	}
	public void setBean(ServerManagerRemote bean) {
		this.bean = bean;
	}
	public FileManagerRemote getFile() {
		return file;
	}
	public void setFile(FileManagerRemote file) {
		this.file = file;
	}
	
	
	public void add(String name){
		file.add(name);
	}
	
	public void delete(String name){
		file.delete(name);
	}
	
	public void findAndDl(String name){
		ServerAddress serv = this.file.find(name, this.file);
		System.out.println(serv.port);
		if(serv!=null)
			copy(name,serv);
	}
	public void copy(String file, ServerAddress from){
		this.file.copy(file, from);
		File fichier = new File(pathLocal + file);
		System.out.println(pathLocal + file);
		try {
			FileServerThread fileServer = new FileServerThread(fichier,  new Socket(from.getHost(),from.getPort()));
			fileServer.start();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void findAndMove(String name){
		ServerAddress serv = this.file.find(name, this.file);
		System.out.println(serv.port);
		if(serv!=null)
			move(name, serv);
	}
	public void move(String file, ServerAddress addr){
		System.out.println("juste apres sa move");
		System.out.println(this.file.move(file, addr));
		File fichier = new File(pathLocal + file);
		System.out.println(pathLocal + file);
		try {
			FileServerThread.receiveFile(fichier, new Socket(addr.getHost(),addr.getPort()));
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<String> getAllFile(){
		return this.file.list();
	}
	
	
	
	public void run(){
	ServerSocket listen;
	try {
		listen = new ServerSocket(s.getPort(), 1);
		while(true){    
	    	System.out.println("Envoie en attente !");
	    	Socket socket = listen.accept();
	    	Thread stuffer = new FileClientThread(socket,pathLocal);
	    	stuffer.start();
	    }
	} catch (IOException e) {
		e.printStackTrace();
	} 
  }
}
